Linux开发

推荐列表 站点导航

当前位置:首页 > 服务器技术 > Linux开发 >

Linux下C++遍历目录下所有文件

来源:互联网  作者:网友投稿  发布时间:2021-01-06 21:56
在Linux下,用 c++ 遍历目录下的所有文件比较简单,代码如下,有需要的可以参考。 #include iostream #include stdio.h #inclu...

在Linux下,用 c++ 遍历目录下的所有文件比较简单,代码如下,有需要的可以参考。

 

#include <iostream>  

#include <stdio.h>  

#include <unistd.h>  

#include <dirent.h>  

#include <stdlib.h>  

#include <sys/stat.h>  

#include <string.h>  

using namespace std;  

/***** Global Variables *****/  

char dir[100] = "/home";  

int const MAX_STR_LEN = 200;  

/* Show all files under dir_name , do not show directories ! */  

void showAllFiles( const char * dir_name )  

{  

// check the parameter !  

if( NULL == dir_name )  

{  

cout<<" dir_name is null ! "<<endl;  

return;  

}  

// check if dir_name is a valid dir  

struct stat s;  

lstat( dir_name , &s );  

if( ! S_ISDIR( s.st_mode ) )  

{  

cout<<"dir_name is not a valid directory !"<<endl;  

return;  

}  

struct dirent * filename;    // return value for readdir()  

DIR * dir;                   // return value for opendir()  

dir = opendir( dir_name );  

if( NULL == dir )  

{  

cout<<"Can not open dir "<<dir_name<<endl;  

return;  

}  

cout<<"Successfully opened the dir !"<<endl;  

/* read all the files in the dir ~ */  

while( ( filename = readdir(dir) ) != NULL )  

{  

// get rid of "." and ".."  

if( strcmp( filename->d_name , "." ) == 0 ||   

strcmp( filename->d_name , "..") == 0    )  

continue;  

cout<<filename ->d_name <<endl;  

}  

}   

int main()  

{  

// 测试  

showAllFiles( dir );  

相关热词:

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/server/kaifa/11621.shtml

相关文章
Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

Linux下C++遍历目录下所有文件

2021-01-06 编辑:网友投稿

在Linux下,用 c++ 遍历目录下的所有文件比较简单,代码如下,有需要的可以参考。

 

#include <iostream>  

#include <stdio.h>  

#include <unistd.h>  

#include <dirent.h>  

#include <stdlib.h>  

#include <sys/stat.h>  

#include <string.h>  

using namespace std;  

/***** Global Variables *****/  

char dir[100] = "/home";  

int const MAX_STR_LEN = 200;  

/* Show all files under dir_name , do not show directories ! */  

void showAllFiles( const char * dir_name )  

{  

// check the parameter !  

if( NULL == dir_name )  

{  

cout<<" dir_name is null ! "<<endl;  

return;  

}  

// check if dir_name is a valid dir  

struct stat s;  

lstat( dir_name , &s );  

if( ! S_ISDIR( s.st_mode ) )  

{  

cout<<"dir_name is not a valid directory !"<<endl;  

return;  

}  

struct dirent * filename;    // return value for readdir()  

DIR * dir;                   // return value for opendir()  

dir = opendir( dir_name );  

if( NULL == dir )  

{  

cout<<"Can not open dir "<<dir_name<<endl;  

return;  

}  

cout<<"Successfully opened the dir !"<<endl;  

/* read all the files in the dir ~ */  

while( ( filename = readdir(dir) ) != NULL )  

{  

// get rid of "." and ".."  

if( strcmp( filename->d_name , "." ) == 0 ||   

strcmp( filename->d_name , "..") == 0    )  

continue;  

cout<<filename ->d_name <<endl;  

}  

}   

int main()  

{  

// 测试  

showAllFiles( dir );  

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/server/kaifa/11621.shtml

相关文章

风云图片

推荐阅读

返回Linux开发频道首页